﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Graphics.Display;
//using Windows.Graphics.Display;
using Windows.UI.Xaml;

/**
 * 创建时间：2014/9/25 12:36:03
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace SurfaceAd.Assistant
{
    /// <summary>
    /// 设备显示信息
    /// <para>Windows.Graphics.Display: GetForCurrentView must be called on a thread that is associated with a CoreWindow.</para>
    /// </summary>
    internal sealed class DeviceScreenHelper
    {
        /// <summary>
        /// Height
        /// <para>窗口大小或视图大小</para>
        /// </summary>
        public static double WindowHeight
        {
            get { return Window.Current.Bounds.Height; }
        }

        /// <summary>
        /// Width
        /// <para>窗口大小或视图大小</para>
        /// </summary>
        public static double WindowWidth
        {
            get { return Window.Current.Bounds.Width; }
        }

        /// <summary>
        /// LogicalDpi
        /// <para>当前设备每逻辑像素所包含的像素数量</para>
        /// </summary>
        public static double LogicalDpi
        {
            get { return DisplayInformation.GetForCurrentView().LogicalDpi; }
        }

        /// <summary>
        /// DpiRatio
        /// <para>每个可视像素对应的实际像素个数，这是WP8.1独有得到属性，WIN8.1使用ResolutionScale来表达</para>
        /// </summary>
        public static double DpiRatio
        {
            get { return DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; }
        }

        /// <summary>
        /// 基准分辨率到实际分辨率的扩展因子
        /// <para>这个属性在WP8.0上为App.Current.Host.Content.ScaleFactor，我们是通过这个扩展因子来判断分辨率是WVGA、WXGA或是720p的。</para>
        /// <para>在WP8.1上都不见了，无法直接获取，但是我们可以这样来计算，[Window.Bounds.Width] * [RawPixelsPerViewPixel] / 480，即我们通过实际的分辨率和基准分辨率来反推</para>
        /// </summary>
        public static double ScaleFactor
        {
            get
            {
                //这个只能支持竖屏
                //return (WindowWidth * DpiRatio) / 480;
                //TODO 要支持横竖屏（这是暂时的方案）
                if (DisplayInformation.GetForCurrentView().CurrentOrientation == DisplayOrientations.LandscapeFlipped || DisplayInformation.GetForCurrentView().CurrentOrientation == DisplayOrientations.Landscape)
                {
                    if (DpiRatio == 1.2 || DpiRatio == 2)
                        return (WindowWidth * DpiRatio) / 800;
                    else
                        return (WindowWidth * DpiRatio) / 853;
                }
                //else if (DisplayInformation.GetForCurrentView().CurrentOrientation == DisplayOrientations.PortraitFlipped || DisplayInformation.GetForCurrentView().CurrentOrientation == DisplayOrientations.Portrait)
                else
                {
                    return (WindowWidth * DpiRatio) / 480;
                }
            }
        }

        /// <summary>
        /// 屏幕分辨率，即实际分辨率
        /// <para>通过[Window.Bounds.Width] * [RawPixelsPerViewPixel]来计算</para>
        /// </summary>
        public static double ScreenResolutionHeight
        {
            get { return Math.Round(WindowHeight * DpiRatio); }
        }


        /// <summary>
        /// 屏幕分辨率，即实际分辨率
        /// <para>通过[Window.Bounds.Height] * [RawPixelsPerViewPixel]来计算</para>
        /// </summary>
        public static double ScreenResolutionWidth
        {
            get { return Math.Round(WindowWidth * DpiRatio); }
        }

        /// <summary>
        /// 逻辑分辨率，即基准分辨率
        /// <para>通过ScreenResolutionHeight/ScaleFactor来计算</para>
        /// </summary>
        public static double LogicalResolutionHeight
        {
            get { return Math.Round(ScreenResolutionHeight / ScaleFactor); }
        }


        /// <summary>
        /// 逻辑分辨率，即基准分辨率
        /// <para>通过ScreenResolutionWidth/ScaleFactor来计算</para>
        /// </summary>
        public static double LogicalResolutionWidth
        {
            get { return Math.Round(ScreenResolutionWidth / ScaleFactor); }
        }

        /// <summary>
        /// 表示屏幕x轴方向上每英寸的实际点数，这个值和实际的屏幕设备密不可分
        /// </summary>
        public static double RawDpiX
        {
            get { return DisplayInformation.GetForCurrentView().RawDpiX; }
        }


        /// <summary>
        /// 表示屏幕y轴方向上每英寸的实际点数，这个值和实际的屏幕设备密不可分
        /// </summary>
        public static double RawDpiY
        {
            get { return DisplayInformation.GetForCurrentView().RawDpiY; }
        }

        /// <summary>
        /// 表示屏幕尺寸，即实际屏幕对角线的长度，单位英寸。
        /// </summary>
        public static double DeviceScreenInch
        {
            get { return Math.Sqrt(Math.Pow(ScreenResolutionHeight / RawDpiY, 2) + Math.Pow(ScreenResolutionWidth / RawDpiX, 2)); }
        }

        public static string OutputDeviceScreenInfo()
        {
            StringBuilder content = new StringBuilder();
            content.AppendLine("Device Screen Info");
            //
            content.Append("CurrentOrientation：").Append("\t").AppendLine(string.Concat(DisplayInformation.GetForCurrentView().CurrentOrientation.ToString()));
            content.Append("Window Size：").Append("\t").AppendLine(String.Concat(WindowHeight.ToString(), " x ", WindowWidth.ToString()));
            content.Append("LogicalDpi：").Append("\t").AppendLine(LogicalDpi.ToString());
            content.Append("DpiRatio(RawPixelsPerViewPixel)：").Append("\t").AppendLine(DpiRatio.ToString());
            content.Append("ScaleFactor：").Append("\t").AppendLine(ScaleFactor.ToString());
            content.Append("Screen Resolution：").Append("\t").AppendLine(String.Concat(ScreenResolutionHeight.ToString(), " x ", ScreenResolutionWidth.ToString()));
            content.Append("Logical Resolution：").Append("\t").AppendLine(String.Concat(LogicalResolutionHeight.ToString(), " x ", LogicalResolutionWidth.ToString()));
            content.Append("RawDpi(X,Y)：").Append("\t").AppendLine(String.Concat(RawDpiX.ToString(), " , ", RawDpiY.ToString()));
            content.Append("DeviceScreenInch：").Append("\t").AppendLine(DeviceScreenInch.ToString());
            //
            return content.ToString();
        }

        /**
         * 1.  Nokia525
        CurrentOrientation：	LandscapeFlipped               CurrentOrientation：	Portrait
        Window Size：	400 x 666.666625976563            Window Size：	666.666625976563 x 400
        LogicalDpi：	115.199996948242                        LogicalDpi：	115.199996948242
        DpiRatio(RawPixelsPerViewPixel)：	1.2                 DpiRatio(RawPixelsPerViewPixel)：	1.2
        ScaleFactor：	1.66666656494141                    ScaleFactor：	1
        Screen Resolution：	480 x 800                        Screen Resolution：	800 x 480
        Logical Resolution：	288 x 480                        Logical Resolution：	800 x 480
        RawDpi(X,Y)：	234 , 236                                RawDpi(X,Y)：	234 , 236
        DeviceScreenInch：	3.97805971949247            DeviceScreenInch：	3.97805971949247
         * 
         * 2.  Nokia1320         
                Window Size	914.285766601563 x 514.285705566406
                LogicalDpi	134.399993896484
                DpiRatio(RawPixelsPerViewPixel)	1.4
                ScaleFactor	1.49999997456868
                Screen Resolution	1280 x 720
                Logical Resolution	853 x 480
                RawDpi(X,Y)	243 , 246
                DeviceScreenInch	5.98773590214341
         * 
         * 3.  Emulator 8.1 1080P 6 inch
                Window Size	872.727294921875 x 490.909088134766
                LogicalDpi	211.199996948242
                DpiRatio(RawPixelsPerViewPixel)	2.2
                ScaleFactor	2.24999998728434
                Screen Resolution	1920 x 1080
                Logical Resolution	853 x 480
                RawDpi(X,Y)	365 , 366
                DeviceScreenInch	6.02283965745542
         * 
         * 4.  Emulator 8.1 1080P 5.5 inch
                Window Size	800 x 450
                LogicalDpi	230.399993896484
                DpiRatio(RawPixelsPerViewPixel)	2.4
                ScaleFactor	2.25
                Screen Resolution	1920 x 1080
                Logical Resolution	853 x 480
                RawDpi(X,Y)	403 , 399
                DeviceScreenInch	5.50794893262816
         * 
         * 5.  Emulator 8.1 720P 4.7 inch
                Window Size	711.111083984375 x 400
                LogicalDpi	172.800003051758
                DpiRatio(RawPixelsPerViewPixel)	1.8
                ScaleFactor	1.5
                Screen Resolution	1280 x 720
                Logical Resolution	853 x 480
                RawDpi(X,Y)	309 , 312
                DeviceScreenInch	4.71809120855649
         * 
         * 6.  Emulator 8.1 WXGA 4.5 inch
                Window Size	640 x 384
                LogicalDpi	192
                DpiRatio(RawPixelsPerViewPixel)	2
                ScaleFactor	1.6
                Screen Resolution	1280 x 768
                Logical Resolution	800 x 480
                RawDpi(X,Y)	330 , 331
                DeviceScreenInch	4.51336069462555
         * 
         * 7.  Emulator 8.1 WVGA 4 inch
                Window Size	666.666687011719 x 400
                LogicalDpi	115.199996948242
                DpiRatio(RawPixelsPerViewPixel)	1.2
                ScaleFactor	1
                Screen Resolution	800 x 480
                Logical Resolution	800 x 480
                RawDpi(X,Y)	234 , 233
                DeviceScreenInch	3.99956475230775
         */
    }
}
